package cn.xinfei.xdecision.data.core.frame.executors.collect.filter;

import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;

import java.util.*;
import java.util.stream.Collectors;


@NoArgsConstructor
@AllArgsConstructor
public class GroovyAfterVarFilter implements VarFilter<String> {

    private Map<String, Set<String>> deriveVarAndParamMap = Maps.newHashMap();
    private Map<String, Set<String>> groovyPreVarAndParamMap = Maps.newHashMap();

    @Override
    public Set<String> actionVar(Set<String> varCodes) {

        Set<String> actionVarList = new HashSet<>();
        if (CollectionUtils.isEmpty(varCodes)) {
            return actionVarList;
        }

        if (MapUtils.isNotEmpty(deriveVarAndParamMap) && MapUtils.isNotEmpty(groovyPreVarAndParamMap)) {

            MapDifference<String, Set<String>> difference = Maps.difference(deriveVarAndParamMap, groovyPreVarAndParamMap);
            Map<String, Set<String>> groovyAfterVarAndParams = difference.entriesOnlyOnLeft();
            if (MapUtils.isNotEmpty(groovyAfterVarAndParams)) {
                actionVarList = varCodes.stream().filter(groovyAfterVarAndParams::containsKey)
                        .collect(Collectors.toSet());
            }

        }
        return actionVarList;
    }
}


